<?php
namespace app\mxty\controller;

use app\api\controller\Controller as ApiController;
use app\mxty\logic\User as UserLogic;
use app\mxty\logic\Booking as BookingLogic;
use app\common\model\Wxapp as WxappModel;
use app\common\library\wechat\WxPay;
use app\mxty\model\BookingFragment as BookingFragmentModel;
use think\facade\Request;

class User extends ApiController
{
    // 获取会员用户订场记录
    public function myBooking()
    {
        $list = (new UserLogic())->getUserBooking();
        return $this->renderSuccess($list->toArray());
    }

    //退款
    public function refund($order_no = null){
        $model = (new BookingLogic())->getOrderByNo($order_no);
        $bookingFragmentModel = new BookingFragmentModel;
        $list = $bookingFragmentModel::where('booking_id', $model -> id)->select();

        $wxConfig = WxappModel::getWxappCache(getStoreId());
        $WxPay = new WxPay($wxConfig);
        $WxPay->refund($model ->transaction_id, $model ->total_price, $model ->total_price);

        //退款
        $model ->pay_status = 30;
        $model -> save();
        foreach($list as $key=>$booking){
            $bookingFragmentModel::update(['pay_status' => 30], ['id' => $booking -> id]);
        }

        return $this->renderSuccess();

    }

    // 获取用户姓名和手机号
    public function getUserInfo()
    {
        $user = (new UserLogic)->getUserInfo();
        return $this->renderSuccess($user);
    }

    // 修改用户姓名和手机号
    public function setUserInfo()
    {
        $params = Request::only([
            'name',
            'mobile'
        ]);

        (new UserLogic)->setUserInfo($params);
        return $this->renderSuccess('修改成功');
    }

    // 检查用户是否有设置姓名和手机号
    public function isSetInfo()
    {
        (new UserLogic)->isSetUserInfo();
        return $this->renderSuccess('信息已设置');
    }
}
